package com.oa.tools.utils;

import java.util.Map;
import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

public class StringUtil {
	public static String underscoreName(String name) {
		StringBuilder result = new StringBuilder();
		if ((name != null) && (name.length() > 0)) {
			result.append(name.substring(0, 1).toUpperCase());

			for (int i = 1; i < name.length(); i++) {
				String s = name.substring(i, i + 1);

				if ((s.equals(s.toUpperCase())) && (!Character.isDigit(s.charAt(0)))) {
					result.append("_");
				}

				result.append(s.toUpperCase());
			}
		}
		return result.toString();
	}

	public static String camelName(String name) {
		StringBuilder result = new StringBuilder();

		if ((name == null) || (name.isEmpty())) {
			return "";
		}
		if (!name.contains("_")) {
			return new StringBuilder().append(name.substring(0, 1).toLowerCase()).append(name.substring(1)).toString();
		}

		String[] camels = name.split("_");
		for (String camel : camels) {
			if (!camel.isEmpty()) {
				if (result.length() == 0) {
					result.append(camel.toLowerCase());
				} else {
					result.append(camel.substring(0, 1).toUpperCase());
					result.append(camel.substring(1).toLowerCase());
				}
			}
		}
		return result.toString();
	}

	public static boolean isBlank(String source) {
		if ((source == null) || (source.trim().length() == 0)) {
			return true;
		}

		return false;
	}

	public static String toString(Object object,boolean notNull,boolean pretty) {
		if (object == null)
			return null;
		if ((object instanceof String)) {
			return object.toString();
		}
		ObjectMapper mapper = new ObjectMapper();
		if(notNull) {
			mapper.setSerializationInclusion(Include.NON_NULL);
		}
		try {
			if (pretty) {
				return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
			}
			return mapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public static String object2String(Object object) {
		return toString(object,false, false);
	}

	public static String parseString(Object object) {
		if(object==null) {
			return null;
		}
		
		return String.valueOf(object);
	}
	
	public String changeToMarkPinYin(String pinYinStr) {
		String tempStr = null;
		try {
			tempStr = PinyinHelper.convertToPinyinString(pinYinStr, " ", PinyinFormat.WITH_TONE_MARK);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tempStr;
	}

	public static String getPinyin(String pinYinStr) {
		String tempStr = null;
		try {
			tempStr = PinyinHelper.convertToPinyinString(pinYinStr, "", PinyinFormat.WITHOUT_TONE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tempStr;
	}
	
	public static String toUpperCaseFirstOne(String s){
		  if(Character.isUpperCase(s.charAt(0)))
		    return s;
		  else
		    return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
		}

	public static String prettifyJsonStr(String jsonStr) throws Exception {
		try {
			Map<String, Object> m = ObjectUtil.json2Map(jsonStr);
			return ObjectUtil.object2JsonPretty(m);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	
	public static String prettifyXmlStr(String xmlStr) throws Exception {
		try {
			Map<String, Object> m = ObjectUtil.xml2Map(xmlStr);
			return ObjectUtil.object2XmlPretty(m);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}
	
	public static String uuid() {
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	public static void main(String[] args) throws Exception {

//		String handler = "车辆采购部部门副总监(李国庆);";
//		handler.substring(handler.indexOf('('),handler.indexOf(')'));
//		System.out.println(handler.substring(handler.indexOf('('),handler.indexOf(')')));
		//System.out.println(uuid());
		System.out.println(System.currentTimeMillis());
		
		
		String s = getPinyin("黄䶮康");
		System.out.println(s);
	}
	

	
}